import Axios from 'axios'
import Vue from 'vue'
import VueRouter from 'vue-router'//vue 路由显示
import Main from '../components/Main'
import Home from '../views/home/Home'
import Login from '../views/login/Login'
import ApprovalAdd from "@/views/talentLog/ApprovalAdd";
import ApprovalLog from "@/views/talentLog/ApprovalLog";
import ApprovalLogSelect from "@/views/talentLog/ApprovalLogSelect";
import ApprovalNeed from "@/views/talentLog/ApprovalNeed";
import ApprovalSelect from "@/views/talentLog/ApprovalSelect";
import LogAdd from "@/views/talentLog/LogAdd";
import LogHistory from "@/views/talentLog/LogHistory";
import Logistics from "@/views/talentLog/Logistics";
import TalentAdd from "@/views/talentLog/TalentAdd";
import TalentHistory from "@/views/talentLog/TalentHistory";
import TalentUpdate from "@/views/talentLog/TalentUpdate";
import Talent from "@/views/talentLog/Talent";
Vue.use(VueRouter)

const routes = [
  {
    path:'/main',
    name:'main',
    component:Main,
    children:[
      {
        path:'/home',
        component:Home
      },
      {
        path: '/appealreportingAdmin',
        name: 'appealreportingAdmin',
        component: () => import('../views/appeal/appealreportingAdmin.vue')
      },
      {
        path: '/completeSuperAdmin',
        name: 'completeSuperAdmin',
        component: () => import('../views/appeal/completeSuperAdmin.vue')
      },
      {
        path: '/completeAdmin',
        name: 'completeAdmin',
        component: () => import('../views/appeal/completeAdmin.vue')
      },
      {
        path: '/completeUserAdd',
        name: 'completeUserAdd',
        component: () => import('../views/appeal/completeUserAdd.vue')
      },
      {
        path: '/completeUser',
        name: 'completeUser',
        component: () => import('../views/appeal/completeUser.vue')
      },
      {
        path: '/appealreportingUser',
        name: 'appealreportingUser',
        component: () => import('../views/appeal/appealreportingUser.vue')
      },
      {
        path: '/appealreportingUserAdd',
        name: 'appealreportingUserAdd',
        component: () => import('../views/appeal/appealreportingUserAdd.vue')
      },
      {
        path: '/appealreportingUserhistory',
        name: 'appealreportingUserhistory',
        component: () => import('../views/appeal/appealreportingUserhistory.vue')
      },
      {
        path: '/appealreportingUserUpdate',
        name: 'appealreportingUserUpdate',
        component: () => import('../views/appeal/appealreportingUserUpdate.vue')
      },
      {
        path: '/appealreportingAdminhistory',
        name: 'appealreportingAdminhistory',
        component: () => import('../views/appeal/appealreportingAdminhistory.vue')
      },
      {
        path: '/appealreportingSuperAdmin',
        name: 'appealreportingSuperAdmin',
        component: () => import('../views/appeal/appealreportingSuperAdmin.vue')
      },
      {
        path: '/appealreportingSuperAdminhistory',
        name: 'appealreportingSuperAdminhistory',
        component: () => import('../views/appeal/appealreportingSuperAdminhistory.vue')
      },
      {
        path: '/approvalAdd',
        component: ApprovalAdd
      },
      {
        path: '/approvalLog',
        component: ApprovalLog
      },
      {
        path: '/approvalLogSelect',
        component: ApprovalLogSelect
      },
      {
        path: '/approvalNeed',
        component: ApprovalNeed
      },
      {
        path: '/approvalSelect',
        component: ApprovalSelect
      },
      {
        path: '/logAdd',
        component: LogAdd
      },
      {
        path: '/logHistory',
        component: LogHistory
      },
      {
        path: '/logistics',
        component: Logistics
      },
      {
        path: '/talentAdd',
        component: TalentAdd
      },
      {
        path: '/talentHistory',
        component: TalentHistory
      },
      {
        path: '/talentUpdate',
        component: TalentUpdate
      },
      {
        path: '/talent',
        component: Talent
      }
    ]
  },
  {
    path:'/',
    component:Login
  }
]



const router = new VueRouter({
  routes
})


//保存token 葛鑫
router.beforeEach((to,from,next)=>{
  //to到哪儿 from从哪离开 next 跳转 为空就放行
  if(to.path==='/'){
    //如果跳转为登录，就放行
    next();
  }else{
    //取出localStorage判断
    let token = localStorage.getItem('tokenkye');
    // alert(token)

    Axios({
      url:'http://localhost:8081/user/dologin',
      method:'get',
      headers:{
        token:token
      }
    }).then((res)=>{
      if(res.data.state===false){
        console.log('请先登录！')
        next({path:'/'})
      }else{
        console.log(res.data)
        next()
      }
    })
  }
})

export default router